1 module unde.games.obj_splitter;
2 
3 import unde.games.obj_loader;
4 import unde.games.collision_detector;
5 import std.algorithm;
6 import std.array;
7 import std.conv;
8 import std.path;
9 import std.stdio;
10 import std.math;
11 
12 import std.string;
13 
14 ObjFile*[SC] split_objfile(ObjFile *obj)
15 {
16     ObjFile*[SC] objs;
17 
18     foreach (object; obj.objects)
19     {
20         foreach (mesh; object.meshes)
21         {
22             foreach (face; mesh.faces)
23             {
24                 SC lt, rb;
25                 foreach (i, p; face)
26                 {
27                     float[3] vert = object.vertices[p.vert];
28                     int X = cast(int)floor((-vert[0]+15.0 - 2.0)/30.0);
29                     int Y = cast(int)floor((vert[1]+8.5 - 2.0)/17.0);
30                     if (i == 0)
31                     {
32                         lt = rb = SC(X,Y);
33                     }
34                     else
35                     {
36                         if (X < lt.x) lt.x = X;
37                         if (Y < lt.y) lt.y = Y;
38                         if (X > rb.x) rb.x = X;
39                         if (Y > rb.y) rb.y = Y;
40                     }
41 
42                     X = cast(int)floor((-vert[0]+15.0 + 2.0)/30.0);
43                     Y = cast(int)floor((vert[1]+8.5 + 2.0)/17.0);
44                     if (X < lt.x) lt.x = X;
45                     if (Y < lt.y) lt.y = Y;
46                     if (X > rb.x) rb.x = X;
47                     if (Y > rb.y) rb.y = Y;
48                 }
49 
50                 foreach (Y; lt.y..rb.y+1)
51                 {
52                     foreach (X; lt.x..rb.x+1)
53                     {
54                         if (SC(X,Y) !in objs)
55                         {
56                             objs[SC(X,Y)] = new ObjFile;
57                             objs[SC(X,Y)].filename = format("models/screen_%02d_%02d.obj", X, Y);
58                             objs[SC(X,Y)].mtl = obj.mtl;
59                         }
60     
61                         if (objs[SC(X,Y)].objects is null || 
62                                 objs[SC(X,Y)].objects[$-1].name != object.name)
63                         {
64                             objs[SC(X,Y)].objects ~= new ObjObject;
65                             objs[SC(X,Y)].objects[$-1].name = object.name;
66                             objs[SC(X,Y)].objects[$-1].vertices = object.vertices;
67                             objs[SC(X,Y)].objects[$-1].texcoords = object.texcoords;
68                             objs[SC(X,Y)].objects[$-1].normals = object.normals;
69                         }
70     
71                         with (objs[SC(X,Y)].objects[$-1])
72                         {
73                             if (meshes is null ||
74                                     meshes[$-1].material != mesh.material ||
75                                     meshes[$-1].smooth != mesh.smooth)
76                             {
77                                 meshes ~= new ObjMesh;
78                                 meshes[$-1].material = mesh.material;
79                                 meshes[$-1].smooth = mesh.smooth;
80                             }
81     
82                             meshes[$-1].faces ~= face;
83                         }
84                     }
85                 }
86             }
87         }
88     }
89 
90     return objs;
91 }